<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="仪表编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ref">
<!--              <a-input v-model="model.ref" placeholder="请输入仪表编号" disabled></a-input>-->
              {{model.ref}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="仪表名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
<!--              <a-input v-model="model.name" placeholder="请输入仪表名称" disabled ></a-input>-->
              {{model.name}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="position">
<!--              <a-input v-model="model.position" placeholder="请输入位置"  disabled></a-input>-->
              {{model.position}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用能分区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zone">
<!--              <j-search-select-tag v-model="model.zone" dict="xsd_unit_part,unit_name,id"  disabled/>-->
              {{model.zone_dictText}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="抄录人" :labelCol="labelCol" :wrapperCol="wrapperCol" >
<!--              <a-input v-model="model.createBy_dictText" placeholder="请输入抄录人"  disabled></a-input>-->
              {{model.createBy_dictText}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="抄录日期" :labelCol="labelCol" :wrapperCol="wrapperCol" >
<!--              <a-input v-model="model.createTime" placeholder="请输入抄录日期"  disabled></a-input>-->
              {{model.createTime}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="!formDisabled">
            <a-form-model-item label="上次表数" :labelCol="labelCol" :wrapperCol="wrapperCol" >
<!--              <a-input v-model="model.lastValue" placeholder=""  disabled></a-input>-->
              {{model.lastValue}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="!formDisabled">
            <a-form-model-item label="上次用量" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <!--              <a-input v-model="model.lastValue" placeholder=""  disabled></a-input>-->
              {{model.lastTotal}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="!formDisabled">
            <a-form-model-item label="本次用量" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <!--              <a-input v-model="model.lastValue" placeholder=""  disabled></a-input>-->
              {{model.endMeterValue-model.lastValue}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="!formDisabled">
            <a-form-model-item label="环比" :labelCol="labelCol" :wrapperCol="wrapperCol" >
<!--              <a-input v-model="model.lastValue" placeholder=""  disabled></a-input>-->
              {{model.lastTotal==0||(model.endMeterValue-model.lastValue)==0?0:((model.endMeterValue-model.lastValue-model.lastTotal)/model.lastTotal*100).toFixed(2)+'%'}}
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="结束表数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endMeterValue">
              <a-input-number v-model="model.endMeterValue" placeholder="请输入结束表数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approveStatus">
<!--              <j-dict-select-tag type="list" v-model="model.approveStatus" dictCode="approve_status" placeholder="请选择审批状态" :disabled="show"/>-->
              <j-search-select-tag
                placeholder="请选择审批状态"
                v-model="model.approveStatus"
                :dictOptions="dictOptions"
                :disabled="show">
              </j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approveRemarks" >
              <a-textarea v-model="model.approveRemarks" rows="4" placeholder="请输入审批备注" :disabled="show"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'UnPowerExportTempForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        show:false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        dictOptions:[
          {
            text:"通过",
            value:"1"
          },{
            text:"驳回",
            value:"0"
          }

        ],
        confirmLoading: false,
        validatorRules: {
          approveStatus:[
            { required: true, message: '请选择审批状态!'},
          ],
        },
        url: {
          add: "/eneMon/unPowerExportTemp/add",
          edit: "/eneMon/unPowerExportTemp/edit",
          queryById: "/eneMon/unPowerExportTemp/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record,text) {
        this.model = Object.assign({}, record);
        if(text=='修改'){
          this.show=true
        }else if(text=='审批'){
          delete this.model.approveStatus
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            //根据show判断修改还是审批
            if(this.show){
              this.model.approveStatus='-1'
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>